﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace JingQiao.Ads.Infrastructure.Data.EF.Repository
{
    using Domain.Model;
    using Domain.Model.DomainObject;
    using Domain.Model.Repository;

    public class ConfigInfoRepository : BaseRepository<IConfigInfo>, IConfigInfoRepository
    {
        public ConfigInfoRepository(IContext context)
            : base(context)
        {
        }

        public bool ContainsDestination(int configInfoId)
        {
            return this._context.Destination.Any(d => d.ConfigInfo.ConfigInfoId == configInfoId);
        }

        public IConfigInfo FindById(int id)
        {
            Check.Argument.IsNotNegativeOrZero(id, "id");

            return this._context.ConfigInfo
                .FirstOrDefault(x => x.ConfigInfoId == id);
        }

        public IQueryable<IConfigInfo> FindByIds(string ids)
        {
            return this._context.ConfigInfo
                .Where("it.ConfigInfoId IN {" + ids + "}");
        }

        public void Remove(string ids)
        {
            var configInfos = FindByIds(ids);
            Remove(configInfos);
        }

        public void Remove(IEnumerable<IConfigInfo> configInfos)
        {
            this._context.DeleteAll(configInfos);
        }

        public IQueryable<IConfigInfo> FindAll()
        {
            return this._context.ConfigInfo
                .OrderBy(t => t.Sequence);
        }

        public PagedResult<IConfigInfo> FindAll(int pageSize, int pageIndex)
        {
            return Find(pageSize, pageIndex, null);
        }

        public PagedResult<IConfigInfo> FindByName(int pageSize, int pageIndex, string nameKeyword)
        {
            if (!string.IsNullOrEmpty(nameKeyword))
                return Find(pageSize, pageIndex, x => x.Name.Contains(nameKeyword));
            return Find(pageSize, pageIndex, null);
        }

        #region 私有方法

        private PagedResult<IConfigInfo> Find(int pageSize, int pageIndex, Expression<Func<IConfigInfo, bool>> predicate)
        {
            IQueryable<IConfigInfo> configinfo = null;

            if (predicate == null)
            {
                configinfo = this._context.ConfigInfo;
            }
            else
            {
                configinfo = this._context.ConfigInfo.Where(predicate);
            }
            var templates = configinfo.OrderBy(t => t.Sequence)
                .Skip(pageSize * pageIndex)
                .Take(pageSize);

            int count = Count(predicate);
            return BuildPagedResult<IConfigInfo>(templates, count);
        }

        private int Count(Expression<Func<IConfigInfo, bool>> predicate)
        {
            if (predicate == null)
                return this._context.ConfigInfo.Count();
            return this._context.ConfigInfo.Where(predicate).Count();
        }

        #endregion
    }
}
